package fr.emse.tatiana.cordtravisualisation;

import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.SelectionEditPolicy;
import org.eclipse.swt.graphics.Color;

import fr.emse.tatiana.graphical.DisplayRule;
import fr.emse.tatiana.replayable.IReplayableModel;
import fr.emse.tatiana.scoresheetvisualisation.EventFigure;
import fr.emse.tatiana.scoresheetvisualisation.ReplayableModelPart;

public class FacetPart extends AbstractGraphicalEditPart {

	private List<CordtraRule> rules;
	private ReplayableFacet elem;


	public FacetPart(ReplayableFacet model, List<CordtraRule> rules2) {
		super();
		setModel(model);
		this.rules = rules2;
		this.elem = model;
	}

	@Override
	protected IFigure createFigure() {
		EventFigure f = new EventFigure();
		return new EventFigure();
	}

    @Override
	protected void refreshVisuals(){
        EventFigure figure = (EventFigure)getFigure();
        ReplayableModelPart parent = ((ReplayableModelPart)getParent());
        long first = elem.replayable.getEventTimestamps().get(0);
        long fromfirst = elem.replayable.getBeginTime(elem.event)-first;
        long duration = elem.replayable.getLastTime(elem.event)-elem.replayable.getBeginTime(elem.event);
        Rectangle r = new Rectangle((parent.millisToCoord(fromfirst))+20,50, parent.millisToCoord(duration+10)+1,10);
        figure.setAbsoluteX(r.x);
        figure.setAbsoluteY(r.y);
        figure.setWidth(r.width);
        figure.setHeight(r.height);
        figure.setBackgroundColor(ColorConstants.white);
        figure.setBorder(new LineBorder());
        figure.setVisible(true);
        figure.setShape(null);
        figure.setToolTip(new Label(elem.replayable.getFacetName(elem.facet)+": "+elem.replayable.getFacetText(elem.event, elem.facet)));
        
        for (CordtraRule rule: rules) {
        	rule.apply(elem, figure);
        }
        figure.repaint();
        ((ReplayableModelPart)getParent()).expandToContain(figure.getBounds());
        
    }
    

	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new SelectionEditPolicy() {

			IFigure feedbackfigure;
			
			@Override
			protected void hideSelection() {
				if (feedbackfigure != null) {
					removeFeedback(feedbackfigure);
				}
				feedbackfigure = null;
			}

			@Override
			protected void showSelection() {
				if (feedbackfigure!=null) {
					return;
				}
				feedbackfigure = new Figure();
				IFigure figure = getHostFigure();
				Rectangle r = figure.getBounds().getExpanded(5, 5);
				feedbackfigure.setBounds(r);
				feedbackfigure.setBorder(new LineBorder(new Color(null, 255, 0, 100),4));
				addFeedback(feedbackfigure);
			}
			
		});

	}

	public Object getEvent() {
		return elem.event;
	}
	
}
